﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace ImageProccessingPractice
{
    public partial class FileBRowser : Form
    {
        delegate void assignList(String[] list);      
        private FileBrowsing fb;

        public FileBRowser()
        {
            InitializeComponent();
            listBox1.Items.Add("..");
            fb = new FileBrowsing(this);
            displayThings();
            
        }

        public void Alert(String s)
        {
            MessageBox.Show(s);
        }
        private void displayThings()
        {
           
            updateFolders(fb.gDirs);
            updateFiles(fb.gFiles);
        }

        private void updateFiles(String[] list)
        {
            if (listBox1.InvokeRequired)
            {
                assignList al = new assignList(updateFiles);
                this.listBox1.Invoke(al, new object[] { list });
            }
            else
            {
                foreach (String s in list)
                    listBox1.Items.Add(s);
            }
        }

        private void updateFolders(String[] list)
        {
            if (listBox1.InvokeRequired)
            {
                assignList al = new assignList(updateFolders);
                this.listBox1.Invoke(al, new object[] { list });
            }
            else
            {
                foreach(String s in list)
                   listBox1.Items.Add(s);
            }
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                String s = listBox1.SelectedItem.ToString();
                if (s.Equals(".."))
                {
                    listBox1.Items.Clear();
                    listBox1.Items.Add("..");
                    fb.goBack();
                    displayThings();

                }
                else
                {
                    FileAttributes file = File.GetAttributes(s);
                    if ((file & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        listBox1.Items.Clear();
                        listBox1.Items.Add("..");
                        fb.gotonewDest(s);
                        displayThings();
                    }
                    else
                    {
                        if (chkExt(s))
                        {
                            new ImageChange(s).Show();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.Alert("Empty Value! :"+ex.Message);
            }
        }

        private Boolean chkExt(String s)
        {
            String t = Path.GetExtension(s);
            switch (t)
            {
                case ".jpg":
                case ".bmp":
                case ".jpeg":
                case ".gif":
                case ".png":
                    return true;
                default:
                    return false;
            }

        }

        
    }
}
